/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.mobiubiq.directory.service.handlers;

import java.util.Hashtable;

/**
 *
 * @author Marnix Renne
 */
public class ServiceHandlerManager {
    private static ServiceHandlerManager instance;
    private Hashtable handlers;
    
    private ServiceHandlerManager() {
        super();
        handlers = new Hashtable();
    }
    
    public static ServiceHandler getHandler(String protocol) {
        return instance()._getHandler(protocol);
    }
    
    public static void register(ServiceHandler handler) {
        instance()._register(handler);
    }
    
    private static ServiceHandlerManager instance() {
        if(instance == null) {
            instance = new ServiceHandlerManager();
        }
        return instance;
    }
    
    private ServiceHandler _getHandler(String protocol) {
        return (ServiceHandler) handlers.get(protocol);
    }
    
    private void _register(ServiceHandler handler) {
        String[] supportedProtocols = handler.getSupportedProtocols();
        for (int i = 0; i < supportedProtocols.length; i++) {
            handlers.put(supportedProtocols[i], handler);
        }
    }
}
